package mq.cinrad.ids.service;

import java.util.HashMap;
import java.util.Map;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;

@Service
public class DataSourceService {
	// @ConfigurationProperties(value = "spring.datasource.druid.one",
	// ignoreInvalidFields = true)
	private static Map<String, DruidDataSource> dataSourceMap = new HashMap<>();

	@Bean
	@ConfigurationProperties(prefix = "spring.datasource.druid.one", ignoreInvalidFields = true)
	public DruidDataSource dataSource() {
		return initDruidDataSource();
	}

	@Bean
	@ConfigurationProperties(prefix = "spring.datasource.druid.two", ignoreInvalidFields = true)
	public DruidDataSource dataSourceTwo() {
		return initDruidDataSource();
	}

	@Bean
	@ConfigurationProperties(prefix = "spring.datasource.druid.three", ignoreInvalidFields = true)
	public DruidDataSource dataSourceThree() {
		return initDruidDataSource();
	}

	@Bean
	@ConfigurationProperties(prefix = "spring.datasource.druid.four", ignoreInvalidFields = true)
	public DruidDataSource dataSourceFour() {
		return initDruidDataSource();
	}

	@Bean
	public Map<String, DruidDataSource> dataSourceMap() {
		return dataSourceMap;
	}

	private DruidDataSource initDruidDataSource() {

		DruidDataSource dc = DruidDataSourceBuilder.create().build();
		dataSourceMap.put(dc.getName(), dc);

		return dc;
	}

}
